package com.ffapp.demo.chat;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ffapp.demo.R;
import com.ffapp.demo.basis.BasisActivity;
import com.ffapp.demo.basis.BasisApp;
import com.ffapp.demo.basis.Constants;
import com.ffapp.demo.bean.BasisBean;
import com.ffapp.demo.bean.LoginBean;
import com.ffapp.demo.bean.WarnInfoData;
import com.ffapp.demo.chat.model.ChatUserListBean;
import com.ffapp.demo.chat.model.ChatUserListData;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

/**
 * 警情列表
 */
public class GroupInfoActivity extends BasisActivity {

    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    private MyAdapterList mAdapter;
    private ChatUserListBean mListBean;

    @BindView(R.id.text_title)
    TextView text_title;
    @BindView(R.id.text_total)
    TextView text_total;
    String alarmId;
    @Override
    public void initViews() {
        setContentView(R.layout.chat_group_infol_activity);
        setTitle("讨论组详情");
        setTitleLeftButton(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setVerticalScrollBarEnabled(true);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mListBean.refresh();
                getList();
            }

            @Override
            public void onLoadMore() {
                getList();
            }
        });
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingMoreEnabled(false);

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        alarmId = getIntent().getStringExtra("id");
        if(TextUtils.isEmpty(alarmId)){
            showToast("ID不存在");
            finish();
            return;
        }
        mListBean = new ChatUserListBean();
        setListView();
        getList();
        setTitleLeftButton(null);
        getDetail();
    }


    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    private void onListViewComplete() {
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();
//        mRecyclerView.setLoadingMoreEnabled(mListBean.hasNextPage());
        mRecyclerView.setLoadingMoreEnabled(false);
//        setEmptyView();
    }

//    private void setEmptyView() {
//        if (mListBean == null || mListBean.getList().size() == 0) {
//            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
//            findViewById(R.id.refresh_view).setOnClickListener(
//                    new View.OnClickListener() {
//                        public void onClick(View v) {
//                            mRecyclerView.refresh();
//                        }
//                    });
//        } else {
//            findViewById(R.id.refresh_view).setVisibility(View.INVISIBLE);
//        }
//    }


    private void getList() {
        MyParams params = new MyParams();
//        params.put(BaseListBeanXF.PAGE_NAME, mListBean.getNextPage());
//        params.put(BaseListBeanXF.PAGE_SIZE_NAME, BaseListBean.PAGE_SIZE);
        params.put("alarmId", alarmId);
        showProgress();
        MyHttpUtils.post(Constants.URL_CHAT_MEMBER_LIST, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {

                        mListBean = (ChatUserListBean) result;
//                        mListBean.addListBean(bean);
                        text_total.setText("共计"+mListBean.getTotal()+"人");
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                        onListViewComplete();
                    }
                },
                0, ChatUserListBean.class);

    }

    private void getDetail() {
        MyParams params = new MyParams();
//        params.put(BaseListBeanXF.PAGE_NAME, mListBean.getNextPage());
//        params.put(BaseListBeanXF.PAGE_SIZE_NAME, BaseListBean.PAGE_SIZE);
        params.put("id", alarmId);
//        params.put("id", "89261");
        params.put("userGroup", LoginBean.getInstance().getUserInfo().getUserGroup());
        MyHttpUtils.post(Constants.URL_FILE_ALARM_INFO_GET_BY_ID, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        BasisBean baseBean = (BasisBean) result;
                        JSONObject a = JSON.parseObject(baseBean.getResultData());
                        JSONArray recordMap = JSON.parseArray(a.getString("list").toString());
                        String warnString = recordMap.get(0).toString();
                        WarnInfoData warnInfoData = JSON.parseObject(warnString, WarnInfoData.class);

//                        WarnInfoListBean bean = (WarnInfoListBean) result;
//                        if(bean.getList().size() ==0)return;
//                        WarnInfoData warnInfoData = bean.getList().get(0);
                        text_title.setText(warnInfoData.getIncidentAddress());
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                    }
                },
                0, BasisBean.class);

    }
    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_group_info_list_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.img_call)
            public ImageView img_call;
            @BindView(R.id.img_photo)
            public ImageView img_photo;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                ChatUserListData data = mListBean.getList().get(position);
                text_name.setText(data.getUserName());
                BasisApp.loadImg(mContext,data.getAvatar(),img_photo,R.drawable.chat_icon_avatar);
                list_item.setTag(position);
            }

        }
    }


    @OnClick(R.id.view_history)
    public void toHistory(){
        Bundle bundle = new Bundle();
        bundle.putString("id", alarmId);
        ActivityTool.skipActivity(mContext, GroupHistoryActivity.class, bundle);
    }

}
